(function() {
    Nokia.ProgressBar = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				value: 0
            };

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance._create();

			instance.setValue(instance.options.value);

			instance.registerVibrationOn([ ]);

			instance.fireCallback('create');
        },

		getValue: function() {
			var instance = this;

			return instance.value;
		},

		setValue: function(value) {
			var instance = this;
			var options = instance.options;
			var Styles = instance.Styles;

			if (value > 100) {
				value = 100;
			}

 			instance.value = value;
			this.valueDiv.width(value + '%');

			var changeClass = (value == 100) ? 'addClass' : 'removeClass';
 			this.valueDiv[ changeClass ](Styles.cornerRight);

			instance.fireCallback('change', value);
			if (value == 100) instance.fireCallback('complete', value);

			if (options.label) {
				instance.label.html(instance.getValue() + '%');
			}
		},

		_create: function() {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;

			instance.element.addClass([
				Styles.progressbar,
				Styles.widget,
				Styles.widgetContent,
				Styles.cornerAll
			].join(' '));

			instance.wrapper = Nokia.dom.parseHTML('<div class="nokia-progressbar-wrapper"></div>');

			if (options.label) {
				instance.label = Nokia.dom.parseHTML('<div class="nokia-progressbar-label">0%</div>');
				instance.wrapper.append(instance.label);
			}

			instance.valueDiv = Nokia.dom.parseHTML(
				'<div class="nokia-progressbar-value ui-state-active ui-widget-header ui-corner-left"></div>'
			);

			Nokia.dom.append(instance.element, instance.valueDiv);

			Nokia.dom.insertBefore(instance.element, instance.wrapper);

			Nokia.dom.append(instance.wrapper, instance.element);

			if (Nokia.util.isNumber(options.width)) {
				instance.element.width(options.width);
			}

			if (Nokia.util.isNumber(options.height)) {
				instance.element.height(options.height);
			}

			// copying dimensions
			instance.wrapper.width( instance.element.width() );
		},

		Styles: {
			progressbar: 'nokia-progressbar',
			widget: 'ui-widget',
			widgetContent: 'ui-widget-content',
			cornerAll: 'ui-corner-all',
			cornerRight: 'ui-corner-right'
		}
    });
})();